<?php

namespace app\index\service;

use app\common\model\ToolModel;
use think\Db;

class TemplateService
{
    //列表
    static public function getTemplateList($param)
    {
        $db = Db::table('setting_template');
        !empty($param['type']) && $db->where('type', $param['type']);
        !empty($param['name']) && $db->where('name', $param['name']);
        !empty($param['field']) && $db->where('field', $param['field']);
        $db->where('status',1)->order('type desc,name desc,sort asc');
        return \app\common\service\ToolService::downOrReturn($db, $param, [], "\app\index\service\TemplateService::templateListHandle");
    }
    //列表处理
    static public function templateListHandle($data){
        $type=[
            1=>'描述模板',
            2=>'标题模板',
        ];
        $required=[
            1=>'是',
            2=>'否',
        ];
        $field_type=[
            1=>'文本框',
            2=>'多选',
            3=>'单选',
            4=>'富文本',
        ];
        foreach ($data as &$v){
            $v['type_str']=$type[$v['type']];
            $v['field_type_str']=$field_type[$v['field_type']];
            $v['required_str']=$required[$v['required']];
        }
        return $data;
    }
    //创建模板
    static public function create($param){
        $tp=ToolModel::find('setting_template',['name'=>$param['name'],'type'=>$param['type'],'field'=>$param['field'],'status'=>1]);
        if($tp){
            return returnArr(2,'模板已存在属性:'.$param['field']);
        }
        if($param['type']==1){
            $param['field_type']=4;
        }
        if($param['type']==2&&$param['field_type']==4){
            return returnArr(2,'标题模板控件类型不能选富文本');
        }
        if($param['field_type']==1||$param['field_type']==4){
            $param['arguments']='';
        }else{
            $param['arguments']=array_values(array_filter(explode(',', preg_replace("/(，)/", ',', $param['arguments']))));
            $param['arguments']=join(',',$param['arguments']);
        }
        $param['creation_time']=TIME;
        $param['operator']=CN_NAME;
        unset($param['s'],$param['id']);
        ToolModel::insertData($param,'setting_template');
        return returnArr();
    }
    //修改模板
    static public function update($param){
        $tp=ToolModel::find('setting_template',['name'=>$param['name'],'type'=>$param['type'],'field'=>$param['field'],'status'=>1,'id'=>['<>',$param['id']]]);
        if($tp){
            return returnArr(2,'模板已存在属性:'.$param['field']);
        }
        if($param['type']==1){
            $param['field_type']=4;
        }
        if($param['type']==2&&$param['field_type']==4){
            return returnArr(2,'标题模板控件类型不能选富文本');
        }
        if($param['field_type']==1||$param['field_type']==4){
            $param['arguments']='';
        }else{
            $param['arguments']=array_values(array_filter(explode(',', preg_replace("/(，)/", ',', $param['arguments']))));
            $param['arguments']=join(',',$param['arguments']);
        }
        $param['creation_time']=TIME;
        $param['operator']=CN_NAME;
        unset($param['s']);
        ToolModel::updateData(['id'=>$param['id']],$param,'setting_template');
        return returnArr();
    }
    static public function delete($param){
        ToolModel::updateData(['id'=>$param['id']],['status'=>2],'setting_template');
        return returnArr();
    }

}